#include "gpu_fast.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>

#include <opencv2/core/ocl.hpp>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>

#include "debug.h"

void test_fast(cv::InputArray _image, std::vector<cv::KeyPoint> &_keypoints,
               cv::Mat &_descriptors, bool bGPU)
{
    if (_image.empty())
    {
        return;
    }

    // 2.图片转Mat
    cv::Mat image = _image.getMat();
    assert(_image.type() == CV_8UC1);

    if (bGPU)
    {
        cv::UMat u_image;
        std::vector<cv::KeyPoint> kpts_max;
        image.copyTo(u_image);
        OCL_GPU_FAST(u_image,kpts_max,20,true);

        printf("kpts_max:%d \r\n", kpts_max.size());
    }
    else
    {
        std::vector<cv::KeyPoint> kpts_max;
        FAST(image, kpts_max, 20, true, cv::FastFeatureDetector::TYPE_9_16);
        printf("kpts_max1:%d \r\n", kpts_max.size());
    }
}

int main(int argc, char **argv)
{

    std::vector<cv::KeyPoint> keypoint;
    cv::Mat _descriptors;

    int iter_count = 100;
    int count = iter_count;
    double sum_time = 0.0f;
    while (iter_count-- > 0)
    {
        cv::Mat src = cv::imread(argv[1], 0);

        if (src.empty())
        {
            return -1;
        }

        Timer timer;
        timer.start();
        test_fast(src, keypoint, _descriptors, true);
        //extract_gpu(src, keypoint, _descriptors);

        double take_time = timer.stop();;
        printf("\r\ngpu take time:%f \r\n", take_time);

        timer.start();
        test_fast(src, keypoint, _descriptors, false);
        //extract_gpu(src, keypoint, _descriptors);
        take_time = timer.stop();;
        printf("\r\ncpu take time:%f \r\n", take_time);

        if (iter_count != count - 1)
            sum_time += take_time;
    }

    printf("\r\nsum_time:%f avg time:%f \r\n", sum_time, sum_time / (count + 1));

    return 0;
}